import cSign from './sign'
import common from '../common/js/common.js'



export default class reqReport {
  constructor({ traCode, version, reqSeq, sysId, sign, signType, data }) {
    this.traCode = traCode
    this.version = version
    this.reqSeq = reqSeq
    this.signType = signType
    this.sysId = sysId
    this.data = data
    this.sign = sign
  }
  toJSONObject() {
    return { "traCode": this.traCode, "version": this.version, "reqSeq": this.reqSeq, "signType": this.signType, "sign": this.sign, "sysId": this.sysId, "data": this.data };
  }
  toData() {
    return this.data;
  }
  toParams() {
    return JSON.stringify(this.toJSONObject());
  }
  getSN() {
    return this.reqSeq;
  }
}
/**获取请求报文 */
export function reqRep(report) {
  var rr = {
    "traCode": ifnull(report.traCode, ""),
    "version": ifnull(report.version, "1"),
    "reqSeq": ifnull(report.reqSeq, getSn(common.sys_id)),
    "signType": ifnull(report.signType, "MD5"),
    "data": ifnullJSON(report.data),
    "sysId": common.sys_id
  }
  if (!report.traCode) {
    sessionStorage.setItem('randImgReqSeq', rr.reqSeq);
  }

  return new reqReport({                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    traCode: rr.traCode,
    version: rr.version,
    reqSeq: rr.reqSeq,
    signType: rr.signType,
    sysId: rr.sysId,
    data: rr.data,
    sign: getSign(toString(rr.traCode, rr.version, rr.reqSeq, rr.signType, rr.sysId, rr.data), rr.signType)
  })
}


function toString(traCode, version, reqSeq, signType, sysId, data) {
  return JSON.stringify({ "traCode": traCode, "version": version, "reqSeq": reqSeq, "signType": signType, "sign": "", "sysId": sysId, "data": data });
}
function ifnull(obj, strDefault) {
  try {
    return obj == null || obj == "" || obj == undefined || obj == "undefined" ? strDefault : obj;
  } catch (e) {
    return strDefault;
  }
}

function ifnullJSON(obj) {
  try {
    return obj == null || obj == "" || obj == undefined || obj == "undefined" ? {} : obj;
  } catch (e) {
    return {};
  }
}

/**
 * 获取签名
 * @param report     待签名的报文
 * @param signType   签名方式
 * @returns {string} 签名
 */
function getSign(report, signType) {
  let strSign = "";
  let salt = cSign.md5("qiqiang");
  switch (signType) {
    case "MD5":
      strSign = cSign.md5(reportToSignString(report) + salt);
      break;
    case "RSA":
      break;
    case "3DES":
      break;
    default:
      strSign = cSign.md5(reportToSignString(report) + salt);
      break;
  }
  return strSign;
}

function reportToSignString(report) {
  var c = report.split("");
  c.sort();
  var SignReport = c.join("");
  console.log(SignReport);
  return SignReport;
}

/**
 * 获取流水号
 * @returns {string} 流水号
 */
function getSn(sysid) {
  return "sn" + sysid + "" + (new Date()).getTime() + "" + parseInt(Math.random() * Math.pow(10, 13));
}